package CH02链表结构.LeetCode链表;

import java.util.ArrayList;

public class LC2058 {
    public int[] nodesBetweenCriticalPoints(ListNode head) {
        int[] ret = new int[]{-1, -1};
        if (head == null || head.next == null || head.next.next == null) {
            return ret;
        }
        ListNode p = head.next;
        ListNode pre = head;
        ListNode next = p.next;
        int id = 2;
        ArrayList<Integer> list = new ArrayList<>();

        while (p != null && next != null) {
            if (pre.val < p.val && next.val < p.val) {
                list.add(id);
            }
            if (pre.val > p.val && next.val > p.val) {
                list.add(id);
            }
            pre = pre.next;
            p = p.next;
            next = next.next;
            id++;
        }

        if (list.size() < 2) {
            return ret;
        }
        ret[1] = list.get(list.size() - 1) - list.get(0);
        ret[0] = ret[1];
        for (int i = 1; i < list.size(); i++) {
            int dis = list.get(i) - list.get(i - 1);
            if (dis < ret[0]) {
                ret[0] = dis;
            }
        }
        return ret;
    }
}